package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.UserHolder;
import com.itheima.reggie.domain.Cart;
import com.itheima.reggie.mapper.CartMapper;
import com.itheima.reggie.service.CartService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class CartServiceImpl implements CartService {

    @Autowired
    private CartMapper cartMapper;

    @Override
    public Cart save(Cart cart) {
        //1. 判断当前cart在数据表中是否存在(查询条件: 套餐/菜品id  UserId)
        //select * from shopping_cart where dish_id = #{菜品id} and userId =#{登录用户id}
        //select * from shopping_cart where setmeal_id = #{套餐id} and userId =#{登录用户id}
        LambdaQueryWrapper<Cart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(cart.getDishId() != null, Cart::getDishId, cart.getDishId());
        wrapper.eq(cart.getSetmealId() != null, Cart::getSetmealId, cart.getSetmealId());
        wrapper.eq(Cart::getUserId, UserHolder.get().getId());

        //这个查询最多可以返回一条数据记录
        Cart cartFromDB = cartMapper.selectOne(wrapper);

        //判断基于上述条件的购物车在数据库中是否存在
        if (cartFromDB != null) {
            //2 如果存在,需要将此记录中得number+1,再更新回数据库
            cartFromDB.setNumber(cartFromDB.getNumber() + 1);
            cartMapper.updateById(cartFromDB);
            return cartFromDB;
        } else {
            //3. 如果不在, 需要构建一个cart对象,保存到数据库
            cart.setUserId(UserHolder.get().getId());
            cart.setNumber(1);
            cart.setCreateTime(new Date());
            cartMapper.insert(cart);
            return cart;
        }

    }

    @Override
    public List<Cart> findList() {
        //查询当前用户的购物车
        LambdaQueryWrapper<Cart> wrapper = new LambdaQueryWrapper<>();
        //select * from shopping_cart where user_id = #{登录用户id}
        wrapper.eq(Cart::getUserId, UserHolder.get().getId());

        return cartMapper.selectList(wrapper);
    }

    @Override
    public void clean() {
        //delete from shopping_cart where user_id = #{登录用户id}
        LambdaQueryWrapper<Cart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Cart::getUserId, UserHolder.get().getId());
        cartMapper.delete(wrapper);
    }

    @Override
    public void updateCount(String dishId, String setmealId) {
        if (StringUtils.isNotEmpty(dishId)) {
            LambdaQueryWrapper<Cart> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Cart::getDishId, dishId);
            Cart cart = cartMapper.selectOne(wrapper);
            if (cart.getNumber() > 1) {
                cart.setNumber(cart.getNumber() - 1);
                cartMapper.updateById(cart);
            } else {
                cartMapper.deleteById(cart.getId());
            }
        } else if (StringUtils.isNotEmpty(setmealId)) {
            LambdaQueryWrapper<Cart> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Cart::getSetmealId, setmealId);
            Cart cart = cartMapper.selectOne(wrapper);
            if (cart.getNumber() > 1) {
                cart.setNumber(cart.getNumber() - 1);
                cartMapper.updateById(cart);
            } else {
                cartMapper.deleteById(cart.getId());
            }
        }
    }
}